package com.example.cardgame.model
const val MISMATCH_PENALTY = 2
const val MATCH_BONUS = 8
const val COST_TO_CHOOSE = 1
class CardMatchingGame( val count: Int) {
    var score = 0
    internal val cards: MutableList<Card>

    init {
        val deck = Deck()
        cards = mutableListOf()
        for (i in 1..count) {
            val card: Card? = deck.drawRandomCard()
            if (card != null) {
                cards.add(card)
            }
        }
    }

    fun reset() {
        val deck = Deck()
        cards.clear()
        for (i in 1..count) {
            val card: Card? = deck.drawRandomCard()
            if (card != null) {
                cards.add(card)
            }
        }
    }

    fun cardAtIndex(index: Int): Card {
        return cards.get(index)
    }

//    fun chooseCardAtIndex(index: Int) {
//        val card= cardAtIndex(index)
//        if (!card.isMatched) {
//            if (card.isChosen) {
//                card.isChosen = false
//            } else {
//                for (otherCard in cards) {
//                    if (otherCard.isChosen && !otherCard.isMatched) {
//                        val matchScore = card.match(arrayOf(otherCard))
//                        if (matchScore > 0) {
//                            score += matchScore * MATCH_BONUS
//                            otherCard.isMatched = true
//                            card.isMatched = true
//                        } else {
//                            score -= MISMATCH_PENALTY
//                            otherCard.isChosen = false
//                        }
//                        break
//                    }
//                }
//                score -= COST_TO_CHOOSE
//                card.isChosen = true
//            }
//        }
//    }
fun chooseCardAtIndex(index: Int) {
    val card = cardAtIndex(index)

    if (!card.isMatched) {
        if (card.isChosen) {
            card.isChosen = false
        } else {
            // 找到已选择但未匹配的卡牌
            val chosenCards = cards.filter { it.isChosen && !it.isMatched }

            if (chosenCards.size == 2) { // 当选择了2张未匹配的卡牌时，开始匹配
                val matchScore = card.match(chosenCards.toTypedArray())
                var additionalPoints = 0 // 额外的点数变量

                // 判断三张牌是否有相同的花色或数字
                val allCards = listOf(card, chosenCards[0], chosenCards[1])
                val hasSameSuit = allCards.map { it.suit }.toSet().size < 3
                val hasSameRank = allCards.map { it.rank }.toSet().size < 3

                // 如果没有相同的花色或数字，增加额外点数
                if (!hasSameSuit && !hasSameRank) {
                    additionalPoints = 10 // 你可以调整这个加分值
                }

                if (matchScore > 0) {
                    score += matchScore * MATCH_BONUS
                    chosenCards.forEach { it.isMatched = true }
                    card.isMatched = true
                } else {
                    score -= MISMATCH_PENALTY
                    chosenCards.forEach { it.isChosen = false }
                }

                // 计算点数和
                score += additionalPoints
                if (score > 15) {
                    score += 5 // 分数超过15，额外加5分
                } else {
                    score -= 5 // 否则扣5分
                }
            }
            score -= COST_TO_CHOOSE
            card.isChosen = true
        }
    }
}


}